# -*- coding=utf-8 -*-
# @Time    : 2018/10/10 22:16
# @Author  : 0x007er
# @File    : main.py
# @Software: PyCharm

from multiprocessing.dummy import Pool as ThreadPool
import pycurl
import StringIO
import time

checkUrl = 'https://xss.app/1.txt'
SuccessList = []
SUCCESS = FAILED = 0
STIME = time.time()

def testProxy(proxy):
    global SuccessList,COUNT,SUCCESS,FAILED
    try:
        c = pycurl.Curl()
        c.setopt(pycurl.URL,checkUrl)
        b = StringIO.StringIO()
        c.setopt(pycurl.WRITEFUNCTION, b.write)
        c.setopt(pycurl.FOLLOWLOCATION, 1)
        c.setopt(pycurl.SSL_VERIFYPEER, 0)
        c.setopt(pycurl.SSL_VERIFYHOST, 0)
        c.setopt(pycurl.CONNECTTIMEOUT, 3)
        c.setopt(pycurl.MAXREDIRS, 5)
        c.setopt(pycurl.PROXY, 'http://'+proxy)
        c.perform()
        res = b.getvalue()
        if res == 'success':
            SuccessList.append(proxy)
            SUCCESS += 1
        else:
            FAILED += 1
    except:
        FAILED += 1
        pass
    NOW = time.time()
    USEDTIME = NOW - STIME
    AVG = float(USEDTIME) / float(SUCCESS+FAILED)
    NEEDTIME = AVG*(COUNT-SUCCESS-FAILED)
    print "USEDTIME:%s\tNEEDTIME:%s\tAVG:%s\tSUCCESS:%s\tFAILED:%s\n" % (round(USEDTIME,2),round(NEEDTIME,2),round(AVG,2),SUCCESS,FAILED)

ProxyList = open('proxy.list').read().strip().split('\n')
COUNT = len(ProxyList)
pool = ThreadPool(100)
pool.map(testProxy, ProxyList)
pool.close()
pool.join()

sf = open('success.list','w')
sf.write('\n'.join(SuccessList))
sf.close()
